package com.linkai.注解和反射.reflect;

/**
 * @Description: 测试 Class 类的创建方式有哪些
 * @Author: 林凯
 * @Date: 2021/10/21 14:57
 */
public class Test02 {
    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Student();
        System.out.println("这个人是：" + person.name);

        // 方式一：通过对象获得
        Class c1 = person.getClass();
        System.out.println(c1.hashCode());

        // 方式二：forname 获得
        Class<?> c2 = Class.forName("com.linkai.注解和反射.reflect.Student");
        System.out.println(c2.hashCode());

        // 方式三：通过类名.Class 获得
        Class c3 = Student.class;
        System.out.println(c3.hashCode());

        // 方式四：基本内置类型的包装类都有一个 Type 类型
        Class c4 = Integer.TYPE;        // Type 类型是 Integer 类对应的基本数据类型 int
        Class c41 = int.class;
        Class c42 = Integer.class;
        System.out.println(c4);
        System.out.println(c41);
        System.out.println(c42);

        // 方式五：获得父类类型
        Class c5 =  c1.getSuperclass();
        System.out.println(c5);

    }
}

class Person{
    String name;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }
}

class Student extends Person {
    public Student(String name) {
        this.name = "学生";
    }

    public Student() {
    }
}

class Teacher extends Person {
    public Teacher() {
    }

    public Teacher(String name) {
        this.name = "老师";
    }
}